<?php

namespace App\Models\Tp;

use Illuminate\Database\Eloquent\Model;

use App\Models;
use App\Models\Member;
use App\Models\School;
use App\Models\Sys;
use App\Models\Subscribe;

class Userrrrr extends Model
{
    protected $table = 'jxt_tp_user';
    protected $primaryKey = 'tp_user_id';

    const CREATED_AT = 'create_at';
    const UPDATED_AT = 'update_at';

    //客户状态
    public static $customer_status_map = [
        '1' => '潜在客户',
        '2' => '初步接触',
        '3' => '持续跟进',
        '4' => '成交客户',
        '5' => '忠诚客户',
        '6' => '无效客户',
    ];

    //重要程度
    public static $importance_map = [
        '1' => '一星',
        '2' => '二星',
        '3' => '三星',
        '4' => '四星',
        '5' => '五星',
        '6' => '六星',
    ];

    public function sysUser()
    {
        return $this->belongsToMany(Models\User\User::class, 'jxt_user_nexus','user_id','other_id')
            ->where('jxt_user_nexus.data_type', '2')
            ->select([
                $this->table.'.tp_user_id',
                'tp_name',
                $this->table.'.telephone',
                'create_at',
                'tp_role_id',
                $this->table.'.customer_status',
                $this->table.'.importance',
                $this->table.'.note',
                'jxt_xp_user.telephone as tel'
            ]);
    }

    //('jxt_user_nexus as a',$this->table.'.tp_user_id','a.other_id')
    public function getTeacher()
    {
        set_time_limit(0);
        return $this->leftJoin('jxt_user_nexus as a',function ($join){
            $join->on($this->table.'.tp_user_id','a.other_id')
                ->where('a.data_type','=','2');
        })
            ->leftJoin('jxt_xp_user as b','b.id','a.user_id')
            ->select([
                $this->table.'.tp_user_id',
                'tp_name',
                $this->table.'.telephone as tel',
                'create_at',
                'tp_role_id',
                $this->table.'.customer_status',
                $this->table.'.importance',
                $this->table.'.note',
                'b.telephone',
            ])->orderBy($this->table.'.tp_user_id','DESC');

        /*return $this->leftJoin('jxt_user_nexus as a', function ($join){
            $join->on($this->table.'.tp_user_id','a.other_id')
            ->where('a.data_type','2');
        })->leftJoin('jxt_xp_user as b','b.id','a.user_id')
            ->select([
                $this->table.'.tp_user_id',
                'tp_name',
                $this->table.'.telephone as tel',
                'create_at',
                'tp_role_id',
                $this->table.'.customer_status',
                $this->table.'.importance',
                $this->table.'.note',
            ])
            ->orderBy($this->table.'.tp_user_id','DESC');*/
    }


    public function vip()
    {
        return $this->belongsToMany(Member\Price::class, 'jxt_member_user','user_id','vip_id')
            ->where('expiry_time', '>', time())
            ->select(['user_id','vip_id','name', 'expiry_time' , 'start_time'])
            ->orderBy('jxt_member_price.id','DESC');
    }

    public function createClasses()
    {
        return $this->hasMany(School\Classes::class,'create_by')
            ->where('status','1');
    }

    public function students()
    {
        return $this->hasMany(School\Student::class,'create_by')
            ->where('status','1');
    }

    //订阅班级
    public function subClass()
    {
        return $this->hasMany(Subscribe\Info::class,'user_id')
            ->where('status','1');
    }

    //孩子
    public function child()
    {
        return $this->hasMany(Sys\Child::class,'create_by')
            ->where('status','1');
    }

}
